﻿using An_Im.Extension.Contract;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PPT = Microsoft.Office.Interop.PowerPoint;

namespace An_Im.Command
{
    static partial class CommandManager
    {
        /// <summary>
        /// 以图形对象排序整个主动画序列
        /// </summary>
        private static void SortSequenceByShape(PPT.Application app, IList<ParaNameValuePair> paralist)
        {
            var sld = CoreManipulator.GetSlideInGeneralOccassion();
            if (sld == null) return;
            var seqc = sld.TimeLine.MainSequence;
            if (seqc.Count <= 1) return;
            var notprevList = new List<int>();
            notprevList.Add(1);

            for (int i = 2; i <= seqc.Count; i++)
            {
                if (seqc[i].Timing.TriggerType != PPT.MsoAnimTriggerType.msoAnimTriggerWithPrevious)
                    notprevList.Add(i);
            }
            notprevList.Add(seqc.Count + 1);

            for (int i = 0; i < notprevList.Count - 1; i++)
            {
                SortNodeByShape(seqc, notprevList[i], notprevList[i + 1] - notprevList[i]);
            }
        }

        /// <summary>
        /// 在断点内排序。
        /// </summary>
        private static void SortNodeByShape(PPT.Sequence seqc, int start, int count)
        {
            if (count <= 1) return;

            var shapes = new Dictionary<PPT.Shape, List<PPT.Effect>>();
            for (int i = start; i < start + count; i++)
            {
                var eff = seqc[i];
                var shp = eff.Shape;
                List<PPT.Effect> list = null;
                if (shapes.ContainsKey(shp))
                {
                    list = shapes[shp];
                }
                else
                {
                    list = new List<PPT.Effect>();
                    shapes.Add(shp, list);
                }
                list.Add(eff);
            }
            foreach (var shpeff in shapes)
            {
                for (int i = 1; i < shpeff.Value.Count; i++)
                {
                    shpeff.Value[i].MoveAfter(shpeff.Value[i - 1]);
                }
            }
        }

        /// <summary>
        /// 以动画类型排序整个主动画序列
        /// </summary>
        private static void SortSequenceByType(PPT.Application app, IList<ParaNameValuePair> paralist)
        {
            var sld = CoreManipulator.GetSlideInGeneralOccassion();
            if (sld == null) return;
            var seqc = sld.TimeLine.MainSequence;
            if (seqc.Count <= 1) return;
            var notprevList = new List<int>();
            notprevList.Add(1);

            for (int i = 2; i <= seqc.Count; i++)
            {
                if (seqc[i].Timing.TriggerType != PPT.MsoAnimTriggerType.msoAnimTriggerWithPrevious)
                    notprevList.Add(i);
            }
            notprevList.Add(seqc.Count + 1);

            for (int i = 0; i < notprevList.Count - 1; i++)
            {
                SortNodeByType(seqc, notprevList[i], notprevList[i + 1] - notprevList[i]);
            }

        }

        /// <summary>
        /// 在断点内排序
        /// </summary>
        private static void SortNodeByType(PPT.Sequence seqc, int start, int count)
        {
            if (count <= 1) return;

            var peffectTypes = new Dictionary<PPT.MsoAnimEffect, List<PPT.Effect>>();
            for (int i = start; i < start + count; i++)
            {
                var eff = seqc[i];
                var effectType = eff.EffectType;
                List<PPT.Effect> list = null;
                if (peffectTypes.ContainsKey(effectType))
                {
                    list = peffectTypes[effectType];
                }
                else
                {
                    list = new List<PPT.Effect>();
                    peffectTypes.Add(effectType, list);
                }
                list.Add(eff);
            }
            foreach (var shpeff in peffectTypes)
            {
                for (int i = 1; i < shpeff.Value.Count; i++)
                {
                    shpeff.Value[i].MoveAfter(shpeff.Value[i - 1]);
                }
            }
        }

        /// <summary>
        /// 快捷触发器。
        /// </summary>
        private static void CreateQuickTrigger(PPT.Application app, IList<ParaNameValuePair> paralist)
        {
            var sld = CoreManipulator.GetSlideInGeneralOccassion();
            var shprange = CoreManipulator.GetShapeRange();
            if (sld == null || shprange == null) return;
            var seqc = sld.TimeLine.InteractiveSequences.Add();
            foreach (PPT.Shape shp in shprange)
            {
                var eff = seqc.AddEffect(shp, PPT.MsoAnimEffect.msoAnimEffectCustom, PPT.MsoAnimateByLevel.msoAnimateLevelNone, PPT.MsoAnimTriggerType.msoAnimTriggerOnShapeClick);
                eff.Timing.TriggerShape = shp;
            }
        }

        /// <summary>
        /// 摆正图形，重设图形的选择框。
        /// </summary>
        private static void ResetShapeSelectionFrame(PPT.Application app, IList<ParaNameValuePair> paralist)
        {
            var sld = CoreManipulator.GetSlideInGeneralOccassion();
            var shprange = CoreManipulator.GetShapeRange();
            if (sld == null || shprange == null) return;
            var shplist = new List<PPT.Shape>();
            foreach (PPT.Shape shp in shprange)
            {
                shplist.Add(shp);
            }

            foreach (var shp in shplist)
            {
                shp.Select(Microsoft.Office.Core.MsoTriState.msoTrue);
                var temp = sld.Shapes.AddShape(Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, shp.Left + shp.Width / 2 - 1, shp.Top + shp.Height / 2 - 1, 2, 2);
                //temp.Fill.ForeColor = shp.Fill.ForeColor;
                shp.PickUp();
                temp.Apply();
                temp.Select(Microsoft.Office.Core.MsoTriState.msoFalse);
                while (temp.ZOrderPosition != shp.ZOrderPosition + 1)
                {
                    temp.ZOrder(Microsoft.Office.Core.MsoZOrderCmd.msoSendBackward);
                }
                shprange = CoreManipulator.GetShapeRange();
                shprange.MergeShapes(Microsoft.Office.Core.MsoMergeCmd.msoMergeUnion, temp);
            }



        }

        /// <summary>
        /// 文本转换成矢量
        /// </summary>
        private static void TextBoxTransfer(PPT.Application app, IList<ParaNameValuePair> paralist)
        {
            var sld = CoreManipulator.GetSlideInGeneralOccassion();
            var shprange = CoreManipulator.GetShapeRange();
            if (sld == null || shprange == null || shprange.Count > 1) return;
            if (shprange[1].Type != Microsoft.Office.Core.MsoShapeType.msoTextBox) return;
            var temp = sld.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal, -0.1f, -0.1f, 0.5f, 0.5f);

            temp.Fill.ForeColor = shprange[1].TextFrame.TextRange.Font.Color;
            temp.Select(Microsoft.Office.Core.MsoTriState.msoFalse);
            shprange = CoreManipulator.GetShapeRange();
            shprange.MergeShapes(Microsoft.Office.Core.MsoMergeCmd.msoMergeFragment, temp);

            int deleteCounter = 0;
            foreach (PPT.Shape shp in sld.Shapes)
            {
                if (deleteCounter >= 2) break;
                if (shp.Left == -0.1f && shp.Top == -0.1f && shp.Width < 0.6f && shp.Height < 0.6f)
                {
                    shp.Delete();
                    deleteCounter++;
                }
            }

            //sld.Shapes[tempid].Delete();
        }


        /// <summary>
        /// 动画预览
        /// </summary>
        private static void AnimationPreview(PPT.Application app, IList<ParaNameValuePair> paralist)
        {
            try
            {
                app.CommandBars.ExecuteMso("AnimationPreview");
            }
            catch
            {
                // MessageBox.Show("这个是预览按钮，你总得有个动画才能成功预览，对吧？");
            }
            //sld.Shapes[tempid].Delete();
        }


        /// <summary>
        /// 圆形排布
        /// </summary>
        private static void CircleArrangement(PPT.Application app, IList<ParaNameValuePair> paralist)
        {
            var sld = CoreManipulator.GetSlideInGeneralOccassion();
            var shprange = CoreManipulator.GetShapeRange();
            if (sld == null || shprange == null) return;

            var shp = shprange[1];
            var rst = int.TryParse(shp.TextEffect.Text, out int count);
            if (rst == false)
            {
                MessageBox.Show("您需要在图形上写一个整数哦");
                return;
            }
            if (count == 1)
            {
                MessageBox.Show("请问一个有什么旋转的必要吗？");
                return;
            }
            var sw = shp.Width; var sh = shp.Height;
            var pwidth = CoreManipulator.App.ActivePresentation.PageSetup.SlideWidth;
            var pheight = CoreManipulator.App.ActivePresentation.PageSetup.SlideHeight;
            var hpw = pwidth / 2;
            var hph = pheight / 2;
            var disdis = (hpw - shp.Left) * (hpw - shp.Left) + (hph - shp.Top) * (hph - shp.Top);
            var dis = Math.Sqrt(disdis);
            var shps = sld.Shapes;
            shp.Copy();
            for (int i = 0; i < count; i++)
            {
                var newshp = shps.Paste();
                var angle = 360d * i / count;
                newshp.Rotation = (float)angle;
                newshp.Left = (float)(hpw + dis * Math.Cos(Math.PI * 2 / 360 * angle)) - sw / 2;
                newshp.Top = (float)(hph + dis * Math.Sin(Math.PI * 2 / 360 * angle)) - sh / 2;
                newshp.TextEffect.Text = "";

            }

        }


        /// <summary>
        /// 录制动画。
        /// </summary>
        private static void RecordAniamtion(PPT.Application app, IList<ParaNameValuePair> paralist)
        {

        }

    }
}
